// textui.go
package main

import (
	"errors"
	"fmt"
	"log"
	"strconv"
)

func startTextUI(heiko *Human) {

	startPlace := heiko.Location()
	fmt.Printf("%s\n", startPlace.Description(heiko))

	log.Printf("heiko: %+v¸\n", heiko)

	var command, parameter1 string
	var n int
	var ok error

	for {
		log.Printf("world: %+v¸\n", world.places)
		fmt.Print("\nYour Command: ")
		n, ok = fmt.Scanln(&command, &parameter1)
		log.Printf("command, parameter1, n, ok after scan:%v, %v, %v, %v", command, parameter1, n, ok)

		switch command {
		case "look": // look around
			fmt.Printf("\n%s\n", heiko.Location().Description(heiko))
		case "go": // goto to another place
			// todo: check for parameter 2?
			direction, error := ParseDirection(parameter1)
			if error != nil {
				fmt.Println("invalid direction")
			}
			destination := heiko.Location().coords.Move(direction)
			if !IsValidLocation(destination) {
				fmt.Println("You can't go that way")
				continue
			}
			heiko.Move(direction)
			fmt.Printf("\n%s\n", heiko.Location().Description(heiko))

			log.Printf("heiko ist hier hin gelaufen: %+v¸\n", heiko)
			log.Printf("main(): Ort von heiko nach move: %v", heiko.Location())
			log.Printf("main(): an diesem ort sind items: %v", heiko.Location().items)

		case "exam": // check an item
			description := heiko.Examine(parameter1)
			fmt.Printf("\n%s\n", description)
		case "take": // take an item
			item, err := ParseEntity(parameter1, heiko.Location())
			if err != nil {
				fmt.Println(err)
				continue
			}
			portable, ok := item.(Portable)
			if !ok {
				fmt.Println("error, item is not portable")
				continue
			}
			heiko.Take(portable)
			fmt.Printf("\n%s\n", heiko.Location().Description(heiko))
		case "drop": // drop an item
			error := heiko.drop(parameter1)
			if error != nil {
				fmt.Println(error)
			} else {
				fmt.Printf("\n%s\n", heiko.Location().Description(heiko))
			}
		case "inv": // inventory
			fmt.Printf("%s\n", heiko.checkInventory())
		case "eat": // eat food
			fmt.Println("Not implemented")
			/* error := heiko.eat(parameter1)
			if error != nil {
				fmt.Println(error)
			}*/
		case "help": // help screen
			// printInstructions()
			fmt.Println("Not implemented")
		case "quit": // quit game
			goto exit
		default:
			fmt.Println("Unknown command, try \"help\" for help")
		}
	}
exit:
}

func ParseDirection(input string) (Direction, error) {

	for direction := range directions {
		if directions[direction].Shortname == input {
			return Direction(direction), nil
		}
	}
	return 0, errors.New("invalid direction")
}

func ParseEntity(input string, location *Place) (Entity, error) {
	index, err := strconv.Atoi(input)
	if err != nil {
		return nil, err
	}
	if index < 0 || index >= len(location.items) {
		return nil, errors.New("error, item not found")
	}
	return location.items[index], nil
}

func getItemByName(name string, items []Entity) Entity {
	for _, item := range items {
		if item.Name() == name {
			return item
		}
	}
	return nil
}
